package cn.itsource.pethome.quartz;

import cn.itsource.pethome.order.domain.OrderProduct;
import cn.itsource.pethome.order.service.IOrderProductService;
import cn.itsource.pethome.pay.domain.PayBill;
import cn.itsource.pethome.pay.service.IPayBillService;
import cn.itsource.pethome.quartz.domain.JobTypeConsts;
import cn.itsource.pethome.quartz.domain.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 定时器执行的任务
 */
public class MainJob implements Job {
    @Autowired
    private IOrderProductService orderProductService;
    @Autowired
    private IPayBillService payBillService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        QuartzJobInfo info = (QuartzJobInfo)jobExecutionContext.getMergedJobDataMap().get("params");
        switch (info.getType()){
            case JobTypeConsts.WAIT_PRODUCT_ORDER_PAY_CANCEL_JOB:
                //操作服务类订单表
                String orderSn = (String) info.getParams().get("orderSn");
                //取消订单
                OrderProduct orderProduct = orderProductService.loadByOrderSn(orderSn);
                //状态更改为取消
                orderProduct.setState(-1);
                orderProductService.update(orderProduct);
                PayBill payBill = payBillService.loadByOrderSn(orderSn);
                payBill.setState(-1);
                payBill.setUpdateTime(new Date());
                payBillService.update(payBill);
                break;
            case  JobTypeConsts.WAIT_ADOPT_ORDER_PAY_CANCEL_JOB:

                break;
            case  JobTypeConsts.WAIT_GOODS_ORDER_PAY_CANCEL_JOB:

                break;
        }
    }
}
